package com.liu.stockmarket.ui.stock

import android.content.Context
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.liu.stockmarket.R
import com.liu.stockmarket.model.Stock
import com.liu.stockmarket.ui.search.SearchResultAdapter
import com.liu.stockmarket.utils.*
import java.math.BigDecimal
import java.time.LocalDate

/**
 * 持有股票适配器
 */
class HoldingStockAdapter :
    BaseQuickAdapter<HoldingStockAdapter.Item, BaseViewHolder>(R.layout.item_holding_stock) {
    override fun convert(holder: BaseViewHolder, item: Item) {
        holder.setText(R.id.tv_stock_name_and_code, "${item.stockName}(${item.stockCode})")
            .setText(R.id.tv_total_money, item.holdingMoney.getString())
            .setText(R.id.tv_now_price, item.nowPrice.getString(4))


        if (item.stockType == Stock.StockType.FUND) {
            holder.setText(R.id.tv_holding_price, item.holdingPrice.getString(4))

        } else {
            holder.setText(R.id.tv_holding_price, item.holdingPrice.getString(3))
        }

        if (BigDecimal.ZERO.setScale(4) == item.holdingNumber.setScale(4)) {
            holder.setText(R.id.tv_holding_number, "清仓")
        }else{
            holder.setText(R.id.tv_holding_number, item.holdingNumber.getString(4))
        }

        val tvTip =holder.getView<TextView>(R.id.tv_tip)
        if (item.dividends != null && item.dividends.setScale(5) != BigDecimal.ZERO.setScale(5)) {
            tvTip.text = "分红："+item.dividends.getString(2)
            tvTip.visibility = View.VISIBLE
        }else{
            tvTip.visibility = View.GONE
        }

        holder.getView<TextView>(R.id.tv_today_profit).money(context, item.todayProfit)
        holder.getView<TextView>(R.id.tv_all_profit).money(context, item.allProfit)

        holder.getView<TextView>(R.id.tv_today_profit_percent)
            .percent(context, item.todayProfitPercent)
        holder.getView<TextView>(R.id.tv_all_profit_percent).percent(context, item.allProfitPercent)


    }

    data class Item(
        val stockId: Long,
        val stockName: String,
        val stockCode: String,
        val stockType: Stock.StockType,
        val stockTransactionExchange: Stock.TransactionExchange,
        val holdingPrice: BigDecimal,
        val holdingNumber: BigDecimal,
        val nowPrice:BigDecimal,
        // 持有市值
        val holdingMoney: BigDecimal,
        // 今日盈亏
        val todayProfit: BigDecimal,
        // 今日盈亏率
        val todayProfitPercent: BigDecimal,
        // 持有收益
        val allProfit: BigDecimal,
        // 持有收益率
        val allProfitPercent: BigDecimal,
        val dividends:BigDecimal?=null,
        val show:Boolean

    )

    class OnItemLongClickListener(
        private val context: Context,
        private val onTransactionListener: ((stock: SearchResultAdapter.Item) -> Unit)? = null,
        private val onKlineListener: ((stock: SearchResultAdapter.Item) -> Unit)? = null,
        private val onDeleteListener: ((stockId: Long) -> Unit)? = null,
        private val onModifyListener: (( stockId: Long,
                                         stockName: String,
                                         stockCode: String,
                                         holdingPrice: BigDecimal,
                                         holdingNumber: BigDecimal) -> Unit)? = null,
        private val onDividendListener: ((stock: SearchResultAdapter.Item,holdingPrice:BigDecimal,holdingNumber:BigDecimal) -> Unit)? = null,
    ) : com.chad.library.adapter.base.listener.OnItemLongClickListener {
        override fun onItemLongClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int): Boolean {
            val item = adapter.getItem(position) as Item

            context.bottomListPopup(
                "", mutableListOf(
                    "走势", "编辑", "加减仓", "分红", "删除"
                )
            ) { i, _ ->
                val stock = SearchResultAdapter.Item(
                    item.stockId,
                    item.stockCode,
                    item.stockName,
                    "",
                    item.stockType,
                    item.stockTransactionExchange,null, transactionId= item.stockId
                )
                when (i) {
                    0 -> onKlineListener?.invoke(stock)
                    1 -> onModifyListener?.invoke(item.stockId,item.stockName,item.stockCode,item.holdingPrice,item.holdingNumber)
                    2 -> {
                        onTransactionListener?.invoke(stock)
                    }
                    3 -> {
                        onDividendListener?.invoke(stock,item.holdingPrice,item.holdingNumber)
                    }
                    4 -> {
                        context.confirm(
                            "操作提示",
                            "将删除相关联的交易数据，是否确认删除！",
                            {
                                onDeleteListener?.invoke(item.stockId)
                            }
                        )
                    }
                }

            }
            return true
        }

    }
}